<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <title>Boost 搜索引擎</title>
    <style>
        /* 去掉网页中所有的内外边距 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        /* 将我们的body内的内容100%和html的呈现吻合 */
        html, body {
            height: 100%;
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
        }
        /* 类选择器.container */
        .container {
            width: 90%;
            margin: 0 auto;
            margin-top: 80px;
        }
        /* 复合选择器，选中container下的search */
        .container .search {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        /* 先选中input标签   直接设置标签的属性，先要选中，input：标签选择器*/
        .container .search input {
            width: 750px;
            height: 60px;
            border: 1px solid #ccc;
            padding-left: 13px;
            color: #333;
            font-size: 20px;
            outline: none;
            border-radius: 20px 0 0 20px;
            transition: border-color 0.3s;
        }
        .container .search input:focus {
            border-color: #4e6ef2;
        }
        /* 先选中button标签   直接设置标签的属性，先要选中，button：标签选择器*/
        .container .search button {
            width: 150px;
            height: 60px;
            background-color: #4e6ef2;
            color: #fff;
            font-size: 20px;
            font-family: 'Arial', sans-serif;
            border: none;
            border-radius: 0 20px 20px 0;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .container .search button:hover {
            background-color: #3a5ac7;
        }
        .container .result {
            width: 100%;
            margin-top: 20px;
        }
        .container .result .item {
            margin-top: 15px;
            background-color: #fff;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .container .result .item a {
            display: block;
            text-decoration: none;
            font-size: 20px;
            color: #4e6ef2;
            transition: color 0.3s;
        }
        .container .result .item a:hover {
            text-decoration: underline;
            color: #3a5ac7;
        }
        .container .result .item p {
            margin-top: 5px;
            font-size: 16px;
            color: #666;
        }
        .container .result .item i {
            display: block;
            font-style: normal;
            color: green;
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="search">
            <input type="text" value="请输入搜索关键字" onfocus="if(this.value == '请输入搜索关键字') { this.value = ''; }" onblur="if(this.value == '') { this.value = '请输入搜索关键字'; }">
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
            <!-- 动态生成网页内容 -->
        </div>
    </div>
    <script>
        // 回车触发搜索
        document.querySelector('.container .search input').addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                Search();
            }
        });

        function Search() {
            // 1.提取数据 $可以理解成就是JQuery的别名
            let query = $(".container .search input").val();
            console.log("query = " + query);

            // 2.发起http请求, ajax: 属于一个和后端进行数据交互的函数， JQuery中的
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function (data) {
                    if(data == '' || data == null){
                        document.write("<h1>没有搜到结果</h1>");
                    }
                    console.log(data);
                    BuildHtml(data);
                }
            });
        }

        function BuildHtml(data) {
            let result_lable = $(".container .result");
            // 清空历史搜索结果
            result_lable.empty();
            for (let elem of data) {
                let a_lable = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    // 跳转到新的页面
                    target: "_blank"
                });
                let p_lable = $("<p>", {
                    text: elem.desc
                });
                let i_lable = $("<i>", {
                    text: elem.url
                });
                let div_lable = $("<div>", {
                    class: "item"
                });
                a_lable.appendTo(div_lable);
                p_lable.appendTo(div_lable);
                i_lable.appendTo(div_lable);
                div_lable.appendTo(result_lable);
            }
        }
    </script>
</body>
</html>